package com.recruit.wechat.controller;


import com.alibaba.druid.util.StringUtils;
import com.recruit.wechat.aop.Token;
import com.recruit.wechat.bean.AdminUser;
import com.recruit.wechat.config.token.JwtUtils;
import com.recruit.wechat.controller.entity.ApiCode;
import com.recruit.wechat.controller.helper.$;
import com.recruit.wechat.controller.model.ApiResultModel;
import io.jsonwebtoken.Claims;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.recruit.wechat.bean.result.ResultPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ResponseBody;

import com.recruit.wechat.bean.WechatUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.Duration;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 肖守凯
 * @since 2020-08-19
 */
@Controller
@RequestMapping("/WeChatUser/wechatUser")
public class WechatUserController extends BaseController{
     /** 主页面 */
    @RequestMapping("admin/main.html")
    public String main(){
       return  "Wechatuser/WechatUserMain";
    }
    /** 添加页面跳转
     * @return 返回页面
     */
    @RequestMapping(value = "admin/addWechatUserHtml")
    public String addWechatUserHtml( ){
             return "Wechatuser/WechatUserAdd";
    }
    /** 编辑页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/editWechatUserHtml")
    public String editWechatUserHtml(Integer id ,Model model ){
           model.addAttribute("wechatUser",wechatUserService.selectWechatUserByIdWechatUser(id));
             return "Wechatuser/WechatUserEdit";
    }

    /** 查看页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/showWechatUserHtml")
    public String showWechatUserHtml(Integer id ,Model model ){
           model.addAttribute("wechatUser",wechatUserService.selectWechatUserByIdWechatUser(id));
             return "Wechatuser/WechatUserShow";
    }


    /** 分页查询WechatUser
     * @param page  页码
     * @param limit  查几条
     * @param wechatUser  前端传进的参数
     * @return 返回统一分页信息
     */
    @RequestMapping(value = "admin/findWechatUserByPage")
    @ResponseBody
    public ResultPage findWechatUserByPage(Integer page, Integer limit,WechatUser wechatUser){
             return wechatUserService.findWechatUserByPage(page,limit,wechatUser);
    }

    /** 添加WechatUser
     * @param wechatUser  前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/addWechatUser")
    @ResponseBody
    public ApiResultModel saveWechatUser(WechatUser wechatUser  ){
             return wechatUserService.addWechatUser(wechatUser);
    }

    /** 编辑WechatUser
     * @param wechatUser  前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/editWechatUser")
    @ResponseBody
    public ApiResultModel editWechatUser(WechatUser wechatUser  ){
             return wechatUserService.editWechatUser(wechatUser);
    }



    /** 删除一条WechatUser
     * @param id  前端传进的ID
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/delWechatUser")
    @ResponseBody
    public ApiResultModel delWechatUser( Integer id  ){
             return wechatUserService.delWechatUser(id);
    }


    @RequestMapping(value = "web/login")
    @ResponseBody
    public ApiResultModel delWechatUser(String phone, String password , HttpSession session){

        WechatUser wechatUser = wechatUserService.login(phone, password);

        Map map=new HashMap();

        map.put("phone",wechatUser.getTrId());
        map.put("email",wechatUser.getTrEmail());

        String token = jwtUtils.createJwt(wechatUser.getTrId(), wechatUser.getTrEmail(), map);


//        redisTemplate.opsForValue().set(token, wechatUser, Duration.ofMinutes(30L));



        return $.success(ApiCode.success,ApiCode.success_msg,token);


    }



    @PostMapping(value = "web/chatUser")
    @ResponseBody

    public ApiResultModel chatUser(HttpServletRequest request) {

        //获取上下文
//        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletResponse response = requestAttributes.getResponse();//响应
//        HttpServletRequest request1= requestAttributes.getRequest();//请求



        //获取token
        String token=request.getHeader("touken");
        System.out.println("token:"+token);
        //判断token是否存在
        if(StringUtils.isEmpty(token)){
            return $.error("请先登录");
        }

        //解析token
        Claims claims = jwtUtils.parseJwt(token);
        Integer id = Integer.valueOf(claims.getId());

        System.out.println("id:"+id);
        String username=String.valueOf(claims.getSubject());
        System.out.println("username:"+username);
        String adminId =String.valueOf(claims.get("adminId"));
        System.out.println("adminId:"+adminId);
        String adminName = String.valueOf(claims.get("adminName"));
        System.out.println("adminName:"+adminName);

        Date date=claims.getExpiration();//获取剩余时间
        System.out.println(date);

        //重新设置登录时间
        System.out.println("时间："+ttl);

        WechatUser wechatUser = wechatUserService.getById(id);

        return $.success(200,"成功",wechatUser);



    }





}
